<?php
/*
 * Created on 22.5.2006
 */
 
class pStructure_Menu extends cGadget 
{	
	
	function getXHTML($view)
	{
		$this->path = $this->cms->tree->getPath($this->cms->view->id);
		$nodes = $this->getNodes($view);		
		
		if (count($nodes)==0) return;
		
		if (($class=$this->getAttr("class"))!="") {
			$class="class=\"".$class."\"";
		}
		foreach ($nodes as $n) 
		{
			$cAdd='';
			foreach ($this->path as $onPath) 
			{
				if ($onPath['id']==$n->id) {
					$cAdd=' selected';
					break;
				} 
			}
			$s[]="<li class=\"".$n->string_id.$cAdd."\" ><a href=\"".$n->getUrl($view)."\">$n->name</a></li>";
		}
		if (count($s)>0) 
		{
			return '<ul '.$class.'>'.implode('',$s).'</ul>';
		} else return '';
	}
	
	function getNodes($view)
	{
		$ch = array();
		$from = $this->getAttr("from");
		
		if ($from == "root") 
		{
			$node = $this->cms->tree->getRoot();
		}
		else if ($from == "this") 
		{
			$node = $this->cms->tree->getNodeById($this->cms->view->id);
		}
		else if (substr($from,0,6)=="level[") 
		{
			// TODO : zrychlit casem
			$level = substr($from,6,-1);
			if (is_numeric($id = $this->path[$level]['id']) && $id!=0) {
				$node = $this->cms->tree->getNode('standard_treeitem.id='.$id);
			} else return array();
		}
		
		
		
		$rel = $this->getAttr("rel");

		$where = $this->getAttr("where");
		if ($where == '') $where = 1;
		
		if ($rel=="children") {
			return $node->getChildrenWhere($where, $view);
		} else if ($rel=="subnodes") {	
			return $node->getSubNodesWhere($where, $view);
		}	
		return array(); 
	}
	
}
 
?>